home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / cvs-1.8 / cvs-1 / cvs-1.8.1 / macintosh / macos_filesys.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-06  |  4.3 KB  |  211 lines

  1. /*
  2.  * macos_filesys.c
  3.  * Filesystem handling stuff for macos
  4.  *
  5.  * Some of this stuff is not "regular", but there are a number of weird
  6.  * conditions that a plain filepath translation didn't seem to handle.
  7.  * For now, this seems to work.
  8.  *
  9.  * Michael Ladwig <mike@twinpeaks.prc.com> --- November 1995
  10.  */
  11.  
  12. #include <cvs.h>
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. static char *macos_fixpath (const char *path);
  19. static char scratchPath[1024];
  20.  
  21. int
  22. macos_mkdir( const char *path, int oflag )
  23. {
  24.     char macPath[1024], *sepCh;
  25.  
  26.     strcpy( macPath, ":" );
  27.     strcat( macPath, path );
  28.     while( (sepCh = strchr(macPath, '/')) != NULL )
  29.         *sepCh = ':';
  30.     
  31.     return mkdir(macPath);
  32. }
  33.  
  34. int
  35. macos_open( const char *path, int oflag, ... )
  36. {
  37.     char macPath[1024], *sepCh;
  38.     
  39.     strcpy( macPath, ":" );
  40.     strcat( macPath, path );
  41.     while( (sepCh = strchr(macPath, '/')) != NULL )
  42.         *sepCh = ':';
  43.  
  44.     return open(macPath, oflag);
  45. }
  46.  
  47. int
  48. macos_chmod( const char *path, mode_t mode )
  49. {
  50.     char macPath[1024], *sepCh;
  51.     
  52.     strcpy( macPath, ":" );
  53.     strcat( macPath, path );
  54.     while( (sepCh = strchr(macPath, '/')) != NULL )
  55.         *sepCh = ':';
  56.  
  57.     return chmod(macPath, mode);
  58. }
  59.  
  60. int
  61. macos_creat( const char *path, mode_t mode )
  62. {
  63.     char macPath[1024], *sepCh;
  64.     
  65.     strcpy( macPath, ":" );
  66.     strcat( macPath, path );
  67.     while( (sepCh = strchr(macPath, '/')) != NULL )
  68.         *sepCh = ':';
  69.  
  70.     return creat(macPath);
  71. }
  72.  
  73. FILE *
  74. macos_fopen( const char *path, const char *mode )
  75. {
  76.     FILE    *fp;
  77.     char    macPath[1024], *sepCh;
  78.     
  79.     strcpy( macPath, ":" );
  80.     strcat( macPath, path );
  81.     while( (sepCh = strchr(macPath, '/')) != NULL )
  82.         *sepCh = ':';
  83.         
  84.     fp = fopen(macPath, mode);
  85.     
  86.     /* Don't know why I'm getting ENOTDIR, but it should be ENOENT */
  87.     
  88.     if( (fp == NULL) && (errno == ENOTDIR) ) errno = ENOENT;
  89.     
  90.     return fp;
  91. }
  92.  
  93. int
  94. macos_chdir( const char *path )
  95. {
  96.     char    macPath[1024], *sepCh;
  97.     int    r;
  98.     
  99.     strcpy( macPath, ":" );
  100.     strcat( macPath, path );
  101.     while( (sepCh = strchr(macPath, '/')) != NULL )
  102.         *sepCh = ':';
  103.  
  104.     r = chdir(macPath+1);
  105.     if( r < 0 )
  106.         return chdir(macPath);
  107.  
  108.     return r;
  109. }
  110.  
  111. int
  112. macos_access(const char *path, int amode)
  113. {    
  114.     return access( macos_fixpath(path), amode );
  115. }
  116.  
  117. DIR *
  118. macos_opendir(const char *path)
  119. {
  120.     FILE    *fp;
  121.     char    macPath[1024], *sepCh;
  122.  
  123.     strcpy( macPath, ":" );
  124.     
  125.     if( strcmp(path, ".") != 0 )
  126.         strcat( macPath, path );
  127.     while( (sepCh = strchr(macPath, '/')) != NULL )
  128.         *sepCh = ':';
  129.  
  130.     return opendir( macPath );
  131. }
  132.  
  133. int
  134. macos_stat (const char *path, struct stat *ststr)
  135. {
  136.     return stat( macos_fixpath(path), ststr );
  137. }
  138.  
  139. int
  140. macos_rename (const char *path, const char *newpath)
  141. {
  142.     char    macPath_from[1024], macPath_to[1024];
  143.  
  144.     strcpy( macPath_from, macos_fixpath(path) );
  145.     strcpy( macPath_to, macos_fixpath(newpath) );
  146.  
  147.     return rename( macPath_from, macPath_to );
  148. }
  149.  
  150. int
  151. macos_unlink (const char *path)
  152. {
  153.     return unlink( macos_fixpath(path) );
  154. }
  155.  
  156. char *
  157. macos_fixpath (const char *path)
  158. {
  159.     char        *sepCh;
  160.     
  161.     strcpy( scratchPath, ":" );
  162.     
  163.     if( (*path == '.') && (*(path+1) == '/') )
  164.         strcat( scratchPath, path+2 );
  165.     else
  166.         strcat( scratchPath, path );
  167.     while( (sepCh = strchr(scratchPath, '/')) != NULL )
  168.         *sepCh = ':';
  169.         
  170.     return scratchPath;
  171. }
  172.  
  173. /* Shamelessly stolen from the OS2 port.  Oddly, only the fopen calls
  174.     seem to respect the binary-text distinction, so I have rewritten
  175.     the code to use fopen, fread, fwrite, and fclose instead of open.    */
  176.     
  177. void
  178. convert_file (char *infile,  int inflags,
  179.           char *outfile, int outflags)
  180. {
  181.     FILE *infd, *outfd;
  182.     char buf[8192];
  183.     int len;
  184.     char iflags[10], oflags[10];
  185.     
  186.     if( inflags & OPEN_BINARY )
  187.         strcpy( iflags, "rb" );
  188.     else
  189.         strcpy( iflags, "r" );
  190.         
  191.     if( outflags & OPEN_BINARY )
  192.         strcpy( oflags, "wb" );
  193.     else
  194.         strcpy( oflags, "w" );
  195.         
  196.     if ((infd = CVS_FOPEN (infile, iflags)) == NULL)
  197.         error (1, errno, "couldn't read %s", infile);
  198.     if ((outfd = CVS_FOPEN (outfile, oflags)) == NULL)
  199.         error (1, errno, "couldn't write %s", outfile);
  200.  
  201.     while ((len = fread (buf, sizeof (char), sizeof (buf), infd)) > 0)
  202.         if (fwrite (buf, sizeof (char), len, outfd) < 0)
  203.         error (1, errno, "error writing %s", outfile);
  204.     if (len < 0)
  205.         error (1, errno, "error reading %s", infile);
  206.  
  207.     if (fclose (outfd) < 0)
  208.         error (0, errno, "warning: couldn't close %s", outfile);
  209.     if (fclose (infd) < 0)
  210.         error (0, errno, "warning: couldn't close %s", infile);
  211. }